package com.situ.company.init;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;

@WebFilter(urlPatterns = {"/web/page/*","/department","/employee"})
public class UserFilter implements Filter{

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest req_ = (HttpServletRequest) req;
        String action = req_.getParameter("action");
        if("reg".equals(action) || "login".equals(action)) {
            chain.doFilter(req, resp);
            return;
        }
        Object user = req_.getSession().getAttribute("user");
        if(user == null) {
            req.getRequestDispatcher("/web/login.jsp").forward(req, resp);
        }else {
            chain.doFilter(req, resp);
        }
    }

}
